<template>
  <div class="container">
    <!--头部查找-->
    <el-row>
      <el-col :span="5">
        <div class="grid-content bg-purple-dark">
          <el-input
            v-model="title"
            style="width:255px;"
            placeholder="用户"
            size="small"
          >
            <el-button @click="search" slot="append">搜索</el-button>
          </el-input>
        </div>
      </el-col>
      <el-col :span="1" :offset="18">
        <div class="grid-content bg-purple-dark">
          <el-button
            type="primary"
            size="small"
            @click="$router.push('admin-edit')"
            >添加</el-button
          >
        </div>
      </el-col>
    </el-row>
    <!--动态头条表格列-->
    <el-table
      :data="list"
      border
      style="width: 100%;margin-top:5px;"
      :height="tableHeight"
    >
      <!-- <el-table-column
        type="selection"
        width="50"
        align="center"
        class="item"
      ></el-table-column> -->
      <el-table-column type="index" width="50"> </el-table-column>
      <el-table-column
        prop="tenant_name"
        label="用户名"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="handle"
        label="操作"
        width="150"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            @click="
              $router.push({
                name: 'AdminEdit',
                query: {
                  id: scope.row.id
                },
                params: {
                  name: scope.row.tenant_name,
                  auth: scope.row.auth_rule
                }
              })
            "
            type="primary"
            size="mini"
            >编辑</el-button
          >
          <el-button
            @click="adminDelete(scope.row.id)"
            type="danger"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div class="table-bottom-bar">
      <div></div>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-size="size"
        layout="total, prev, pager, next"
        :total="total"
        :background="true"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getAdminList, adminDelete } from "@/api/auth-mng";
export default {
  name: "AuthMngIndex",
  data() {
    return {
      title: "",
      list: [],
      dialogVisible: false,
      searchStr: "",
      total: 0,
      size: 0,
      page: 1,
      tableHeight: 0,
      currentAdmin: null
    };
  },
  methods: {
    adminDelete(id) {
      this.$confirm("确认删除该用户?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        adminDelete({ id }).then(res => {
          if (res.code === 200) {
            this.getAdminList(this.page);
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        });
      });
    },
    search() {
      this.getAdminList(1);
    },
    getAdminList(page) {
      getAdminList({
        page,
        condition: this.title ? this.title : undefined
      }).then(res => {
        if (res.code === 200) {
          this.list = res.data.data;
          this.total = res.data.total;
          this.page = res.data.current_page;
          this.size = res.data.per_page;
        }
      });
    },
    handleCurrentChange(page) {
      this.getDynamicList(page);
    },
    changeSize() {
      this.tableHeight = document.body.clientHeight - 200;
      window.onresize = () => {
        this.changeSize();
      };
    }
  },
  created() {
    this.getAdminList(1);
    this.changeSize();
  }
};
</script>

<style lang="scss" scoped></style>
